using NUnit.Framework;
using ValidationFramework;
using ValidationFramework.Configuration;

namespace ExampleLibraryCSharp.Configuration
{
	public class CustomRuleConfigReaderExample
	{
		public class Person
		{

			public string Name
			{
				get;
				set;
			}
			private static void ValidateName(object sender, CustomValidationEventArgs e)
			{
				var name = (string)e.TargetMemberValue;
				if (string.IsNullOrEmpty(name))
				{
					e.IsValid = false;
				}
				else
				{
					e.IsValid = true;
				}
			}
		}

		[TestFixture]
		public class ExampleUsage
		{

			private const string propertyXmlString = @"<?xml version='1.0' encoding='utf-8' ?>
<validationMapping xmlns='urn:validationFramework-validationDefinition-1.5'>
  <class typeName='ExampleLibraryCSharp.Configuration.CustomRuleConfigReaderExample+Person, ExampleLibraryCSharp'>
    <property name='Name'>
      <rule typeName='CustomRule' 
				ruleInterpretation='This is a custom rule' 
				validationMethod='ValidateName' 
				validationTypeName='ExampleLibraryCSharp.Configuration.CustomRuleConfigReaderExample+Person, ExampleLibraryCSharp' 
				errorMessage='hello' />
    </property>
  </class>
</validationMapping>";

			[Test]
			public void RunCustomRuleConfigReader()
			{
				ConfigurationService.AddXmlString(propertyXmlString);
				var person = new Person();
				var propertyValidationManager = new PropertyValidationManager(person);
				propertyValidationManager.ValidateAll();
				Assert.IsFalse(propertyValidationManager.IsValid);
				person.Name = "John";
				propertyValidationManager.ValidateAll();
				Assert.IsTrue(propertyValidationManager.IsValid);
			}
		}
	}
}